

#ifndef TIMERS_H
#define TIMERS_H

//Timer and Servo Control Stuff

#define TPM2_PRESCALE_FACTOR              (8)
#define SERVO_REFRESH_TIME                (.02)        

#define RPM_TIMEOUT                       2
#define MAX_RPM_TIMEOUT_COUNT             (WORD)((DWORD)(BUS_CLOCK * RPM_TIMEOUT)>>16)

#define STEERING_SERVO_MIN_TIME           (.0012)
#define STEERING_SERVO_NEUTRAL_TIME       (.0015)
#define STEERING_SERVO_MAX_TIME           (.0018)

#define STEERING_SERVO_NEUTRAL            ((WORD)((BUS_CLOCK/TPM2_PRESCALE_FACTOR)*STEERING_SERVO_NEUTRAL_TIME))
#define STEERING_SERVO_MIN                ((WORD)((BUS_CLOCK/TPM2_PRESCALE_FACTOR)*STEERING_SERVO_MIN_TIME))
#define STEERING_SERVO_MAX                ((WORD)((BUS_CLOCK/TPM2_PRESCALE_FACTOR)*STEERING_SERVO_MAX_TIME))

#define DRIVE_SERVO_MIN_TIME           (.001)
#define DRIVE_SERVO_NEUTRAL_TIME       (.0015)
#define DRIVE_SERVO_MAX_TIME           (.002)

#define DRIVE_SERVO_NEUTRAL            ((WORD)((BUS_CLOCK/TPM2_PRESCALE_FACTOR)*DRIVE_SERVO_NEUTRAL_TIME))
#define DRIVE_SERVO_MIN                ((WORD)((BUS_CLOCK/TPM2_PRESCALE_FACTOR)*DRIVE_SERVO_MIN_TIME))
#define DRIVE_SERVO_MAX                ((WORD)((BUS_CLOCK/TPM2_PRESCALE_FACTOR)*DRIVE_SERVO_MAX_TIME))

extern WORD SteeringServoNeutralPosition;
extern WORD DriveServoNeutralPosition;
extern volatile BOOL ControlSystemTick;


typedef union
{
   DWORD FullValue;
   
   struct
   {
    WORD Hi;
    WORD Low;
   
   } Pieces;

} RPMTickHolder;



void InitServos(void);
void SetSteeringServo(SIGNED_WORD ServoVal);
void SetDriveServo(SIGNED_WORD ServoVal);
void InitRPMMeasurement(void);

#define SYSTEM_TICK_TIME


#define QUARTER_SECOND     (12)
#define HALF_SECOND       (25)
#define ONE_SECOND        (HALF_SECOND*2)
#define TWO_SECONDS       (ONE_SECOND*2)

void Block(WORD Ticks);
void InitTimerTPM1(void);
void InitTimerTPM2(void);
  

extern RPMTickHolder CurrentRPMMeasurement;
   
extern volatile BYTE HostUpdateTimer;


#define NUM_GP_TIMERS 3
extern volatile WORD GPTimer[NUM_GP_TIMERS];

#endif

